package cn.jekun.shop.web.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.MyShopService;
import cn.jekun.shop.common.service.ShopBaseAction;

public class MyShopAction extends ShopBaseAction{
	
	@Resource(name=MyShopService.BEAN_NAME)
	MyShopService myShopService;
	
	/**
	 * 返回当前登录用户的“我的商家”
	 * @return
	 */
	public String getMyShops(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			String userId = this.getUserId();
			int ps=10,p=1;
			try {
				ps = Integer.parseInt(pageSize);
			} catch (Exception e) {
			}
			try {
				p = Integer.parseInt(currentPage);
			} catch (Exception e) {
			}
			page.setPageSize(ps);
			page.setPageNumber(p);
			
			jsonObject = myShopService.getMyShops(userId,page);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
}
